import {
	bluetoothStart
} from "@/static/bluetooth/core.js"
import {
	getCarListAPI,
} from "@/api/api/car.js"
import {
	getUnReadCountAPI
} from "@/api/api/message.js"

import {
	$getCarDetail,
	mqttMSG,
	client,
} from "@/utils/utils.js"

import {
	dataPointMqttTopic,
	fenceMqttTopic,
	authTopic,
} from '@/api/http/setting';

import {
	getStoreSyncUtil
} from "@/utils/store.js"

export default {
	data() {
		return {
			isOnload: true, //正式环境
		}
	},
	methods: {
		//初始化页面，处理蓝牙连接、设备详情、和mqtt连接，未读消息数量
		initIndex() {
			
			let currentBike = getStoreSyncUtil('currentBike');
			let targetCarStatus = getStoreSyncUtil("targetCarStatus");
			let shakeValue = getStoreSyncUtil("shakeValue");
			
				let pages = getCurrentPages();
			console.log("当前页面",pages);
			//防止多次扫描进入和搜索进入停留在登录页
			if(pages.length>0){
				if(pages[0].$page.fullPath == "/pages/views/home/start"){
					this.isOnload = true;
				}
			}
			
			this.$store.commit("setShakeValue", shakeValue || {});
			
			if (!this.$validatenull(targetCarStatus)) {
				this.$store.commit("setTargetCarStatus", targetCarStatus);
			}
			//有默认设备
			if (!this.$validatenull(currentBike)) {

				//有默认设备,但是没有找到此设备，防止后台删除数据或者解绑数据
				getCarListAPI().then(([err, data]) => {
					if (err) return;
					let list = data.records;
					let flag = false;
					for (let i = 0; i < list.length; i++) {
						if (list[i].id == currentBike.id) {
							flag = true;
							break;
						}
					}
					if (flag) {
						this.$store.commit("setCurrentBike", currentBike);
						this.initData(currentBike, () => {
							if (this.isOnload) { //如果是重启小程序，跳转到首页，否则不管
								this.$switchTab("/pages/views/home/index");
								this.isOnload = false;
							}
						});
					} else {
						if (list.length) {
							let currentBike = {
								id: list[0].id,
								ecuId: list[0].ecuId,
							}
							this.$store.commit("setCurrentBike", currentBike);
							this.initData(list[0], () => {
								if (this.isOnload) {
									this.$switchTab("/pages/views/home/index");
									this.isOnload = false;
								}
							});
						} else {
							this.$store.commit("setCurrentBike", {});
							this.$store.commit("setDeviceData", {});
							mqttMSG();
							if (this.isOnload) {
								this.$switchTab("/pages/views/home/index");
								this.isOnload = false;
							}
						}
					}

				});

			} else { //没有默认设备，查询设备列表，设置第一个为默认设备
				getCarListAPI().then(([err, data]) => {
					if (err) return;
					let list = data.records;
					if (list.length) {
						let currentBike = {
							id: list[0].id,
							ecuId: list[0].ecuId,
						}
						this.$store.commit("setCurrentBike", currentBike);
						this.initData(list[0], () => {
							if (this.isOnload) {
								this.$switchTab("/pages/views/home/index");
								this.isOnload = false;
							}
						});
					} else {
						this.$store.commit("setCurrentBike", {});
						this.$store.commit("setDeviceData", {});
						mqttMSG();
						if (this.isOnload) {
							this.$switchTab("/pages/views/home/index");
							this.isOnload = false;
						}
					}


				})
			}
			getUnReadCountAPI(this.$store.state.loginUser.user_id).then(([err, data]) => {
				if (err) return;
				this.$store.commit("setUnReadNum", data.unReadNum)
			});
			

		},
		//初始化数据
		initData(currentBike, callback) {
			//获取设备详情
			$getCarDetail(currentBike.id, () => {
				this.$store.commit("setBlueCurrentToConnectDevice", {
					ecuId: currentBike.ecuId
				})
				//判断当前哪一个页面
				callback();
				//蓝牙连接
				bluetoothStart();
				//mqtt连接
				mqttMSG();
			});
		},
		$switchTab(url) {
			uni.switchTab({
				url: url
			});
		}
	}
}
